package com.guigu.order.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.order.entity.OrderCargo;
import com.guigu.order.entity.OrderLocation;
import com.guigu.order.entity.dto.OrderDTO;
import com.guigu.order.entity.vo.OrderCargoVO;
import com.guigu.order.entity.vo.OrderLocationVO;
import com.guigu.order.service.OrderLocationService;
import com.guigu.tms.exception.TmsException;
import com.guigu.tms.result.R;
import com.guigu.tms.result.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单位置信息  前端控制器
 * </p>
 *
 * @author TaoLong
 * @since 2021-11-09
 */
@RestController
@RequestMapping("/order/order-location")
@Api(tags = "订单位置管理")
public class OrderLocationController {

    @Autowired
    private OrderLocationService orderLocationService;


    @ApiOperation("获取两地之间的距离")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "begin", value = "起点"),
            @ApiImplicitParam(name = "end", value = "终点")
    })
    @GetMapping("/getDistance/{begin}/{end}")
    public R<Map> getDistance(@PathVariable String begin,
                              @PathVariable String end) {
        Map<String, Object> map = orderLocationService.getDistance(begin, end);
        return new R<>(ResultCode.SUCCESS_CODE, "距离获取成功!", map);
    }


    @ApiOperation("订单位置添加")
    @ApiImplicitParam(
            name = "orderDTO",
            value = "订单位置数据传输对象包含(订单信息,订单重量信息,订单距离信息)")
    @PostMapping("/")
    public R save(@RequestBody OrderDTO orderDTO) {
        judgeNonEmpty("订单位置信息不能为null", ObjectUtils.isEmpty(orderDTO));
        orderLocationService.save(orderDTO.getOrderLocation());
        return new R(ResultCode.SUCCESS_CODE, "订单位置信息添加成功!", true);
    }


    @ApiOperation("订单位置修改")
    @ApiImplicitParam(
            name = "orderDTO.getOrderCargo()",
            value = "订单位置信息"
    )
    @PutMapping("/")
    public R update(@RequestBody OrderDTO orderDTO) {
        if (ObjectUtils.isEmpty(orderDTO)) {
            throw new TmsException(ResultCode.ERROR_CODE, "订单位置修改参数为null");
        }
        orderLocationService.updateById(orderDTO.getOrderLocation());
        return new R(ResultCode.SUCCESS_CODE, "订单位置修改成功!", true);
    }

    @ApiOperation("订单位置删除")
    @ApiImplicitParam(name = "id", value = "订单位置Id")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable String id) {
        judgeNonEmpty("订单位置Id不能为null", StringUtils.isBlank(id));
        orderLocationService.removeById(id);
        return new R(ResultCode.SUCCESS_CODE, "订单删除成功!", true);
    }

    @ApiOperation("根据订单id查找订单位置信息")
    @ApiImplicitParam(name = "id", value = "订单Id")
    @GetMapping("/{id}")
    public R<OrderLocationVO> queryById(@PathVariable String id) {
        judgeNonEmpty("订单Id不能为null", StringUtils.isBlank(id));
        QueryWrapper<OrderLocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_id", id);
        OrderLocation cargo = orderLocationService.getOne(queryWrapper);
        OrderLocationVO cargoVO = new OrderLocationVO();
        BeanUtil.copyProperties(cargo, cargoVO);
        return new R<>(ResultCode.SUCCESS_CODE, cargoVO);
    }

    /**
     * 通过当前机构和目的机构来获取数据
     *
     * @param sendAgencyId 当前机构
     * @param receiveId    目的机构
     * @return
     */
    @ApiOperation("通过当前机构和目的机构来获取数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sendAgencyId", value = "寄件机构Id"),
            @ApiImplicitParam(name = "receiveId", value = "接收机构Id")
    })
    @GetMapping("/{sendAgencyId}/{receiveId}")
    public R<List<OrderLocation>> queryOrderLocationBySendAgencyIdAndReceiveId(@PathVariable String sendAgencyId, @PathVariable String receiveId) {
        QueryWrapper<OrderLocation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("send_agent_id", sendAgencyId);
        queryWrapper.eq("receive_agent_id", receiveId);
        List<OrderLocation> list = orderLocationService.list(queryWrapper);
        return new R<>(ResultCode.SUCCESS_CODE, list);
    }

    /**
     * 判断非空
     *
     * @param message 异常消息
     * @param empty   是否为null
     */
    private void judgeNonEmpty(String message, boolean empty) {
        if (empty) {
            throw new TmsException(ResultCode.ERROR_CODE, message);
        }
    }
}

